﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using W.Product.API.Appliaction.Command.ProductCommand;
using W.Product.API.Appliaction.Command.ProductTypeCommand;
using W.Product.Domain.Product;

namespace W.Product.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ProductTypeController : ControllerBase
    {
        private readonly IMediator mediator;
        private ILogger<ProductTypeController> logger;

        public ProductTypeController(IMediator mediator, ILogger<ProductTypeController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 添加商品类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProductTypes(ProductTypeCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("添加产品");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }


        /// <summary>
        /// 修改产品类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpProductTypes(ProductTypeIsUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("修改产品类别");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }

        }

        /// <summary>
        /// 删除产品类别
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelProductTypes(ProductTypeIsDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("删除产品类别");
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }

        }
        /// <summary>
        /// 反填产品类别
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductType>> FindProductTypes([FromQuery] ProductTypeFindlCommand product)
        {
            ApiResult<ProductType> result = new ApiResult<ProductType>();
            try
            {
                logger.LogInformation("反填产品管理");
                result.Data = await mediator.Send(product);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }

        /// <summary>
        /// 删除产品类别
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> IsDelProduct(ProductTypePilDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("删除产品类别");
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }

        }
    }
}
